//
//  HPQualityPhotoViewController.m
//  HPZZTong
//
//  Created by Handpay on 14-3-29.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPQualityPhotoViewController.h"

@interface HPQualityPhotoViewController ()

@end

@implementation HPQualityPhotoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"提交照片资料";
    [super viewDidLoad];
    
    CGRect frame = self.contentScrollView.frame;
    frame.size.height = 416;
    if (HP_Device_iPhone5) {
        frame.size.height += IPHONE5_ADD_HEIGHT;
    }
    self.contentScrollView.frame= frame;
    
    AccountInfoData* accountInfo = ZZT_CLIENG.accountInfo;
    
    if (accountInfo.accountStatus == AccountStatusFailed) {
        self.errorView.hidden = NO;
        self.errorView.errorMsg = accountInfo.statusDescpriction;
        [self.errorView refresh];
    }
    
    [self.nextButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.nextButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
    
//    self.contentScrollView.contentSize = CGSizeMake(ScreenWidth, 504);
    self.certBackButton.exclusiveTouch = self.certFrontButton.exclusiveTouch = self.headButton.exclusiveTouch = self.shopButton.exclusiveTouch = self.licenseButton.exclusiveTouch = self.nextButton.exclusiveTouch = YES;
    
    self.certBackButton.presentScreen = self.certFrontButton.presentScreen = self.headButton.presentScreen = self.shopButton.presentScreen = self.licenseButton.presentScreen = self;
    
    self.certBackButton.delegate = self.certFrontButton.delegate = self.headButton.delegate = self.shopButton.delegate = self.licenseButton.delegate = self;
    
    if (accountInfo.accountStatus == AccountStatusFailed
        || accountInfo.accountStatus == AccountStatusSuccess) {
        [self.certFrontButton setMsg:@"身份证正面照" imageName:@"idcardphoto.png" status:accountInfo.frontCCPicStatus helpImageName:@"idcardphoto_large.png" url:accountInfo.frontCCPicUrl];
        [self.certBackButton setMsg:@"身份证反面照" imageName:@"idcardbackphoto.png" status:accountInfo.backCCPicStatus helpImageName:@"idcardbackphoto_large.png" url:accountInfo.backCCPicUrl];
        [self.headButton setMsg:@"手持身份证照" imageName:@"withidcardphoto.png" status:accountInfo.holdPicStatus helpImageName:@"withidcardphoto_large.png" url:accountInfo.holdPicUrl];
        [self.shopButton setMsg:@"营业场所照片" imageName:@"placephoto.png" status:accountInfo.companyPicStatus helpImageName:@"shoppic_large.png" url:accountInfo.companyPicUrl];
        [self.licenseButton setMsg:@"营业执照照片" imageName:@"placephoto.png" status:accountInfo.licensePicStatus helpImageName:@"business_license_large.png" url:accountInfo.licensePicUrl];
    }else{
        [self.certFrontButton setMsg:@"身份证正面照" imageName:@"idcardphoto.png" status:PhotoStatusNotSubmit helpImageName:@"idcardphoto_large.png"];
        [self.certBackButton setMsg:@"身份证反面照" imageName:@"idcardbackphoto.png" status:PhotoStatusNotSubmit helpImageName:@"idcardbackphoto_large.png"];
        [self.headButton setMsg:@"手持身份证照" imageName:@"withidcardphoto.png" status:PhotoStatusNotSubmit helpImageName:@"withidcardphoto_large.png"];
        [self.shopButton setMsg:@"营业场所照片" imageName:@"placephoto.png" status:PhotoStatusNotSubmit helpImageName:@"shoppic_large.png"];
        [self.licenseButton setMsg:@"营业执照照片" imageName:@"placephoto.png" status:PhotoStatusNotSubmit helpImageName:@"business_license_large.png"];
    }
    
    CGRect contentFrame = self.buttonView.frame;
    if (!self.errorView.hidden) {
        contentFrame.origin.y = self.errorView.frame.origin.y + self.errorView.frame.size.height;
    }else{
        
    }
    self.buttonView.frame = contentFrame;
    
    self.contentScrollView.contentSize = CGSizeMake(ScreenWidth, contentFrame.origin.y + contentFrame.size.height);
}

- (void)dealloc {
    [_contentScrollView release];
    [_certFrontButton release];
    [_certBackButton release];
    [_headButton release];
    [_shopButton release];
    [_licenseButton release];
    [_nextButton release];
    [_buttonView release];
    [_errorView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setContentScrollView:nil];
    [self setCertFrontButton:nil];
    [self setCertBackButton:nil];
    [self setHeadButton:nil];
    [self setShopButton:nil];
    [self setLicenseButton:nil];
    [self setNextButton:nil];
    [self setButtonView:nil];
    [self setErrorView:nil];
    [super viewDidUnload];
}

- (BOOL)checkPhoto:(ZZTongPhotoButton*)button errMsg:(NSString*)errMsg dic:(NSMutableDictionary*)dic key:(NSString*)key{
//    if (button.buttonStatus == PhotoStatusSuccess) {
//        return YES;
//    }
    
    if (!button.photoImageView.image) {
        [HPUIENG showAlert:[NSString stringWithFormat:@"请拍摄%@", errMsg]];
        return NO;
    }
    
//    if (button.image && [NSString isNilOrEmpty:button.imgStr]) {
//        [HPUIENG showAlert:[NSString stringWithFormat:@"请重新拍摄%@", errMsg]];
//        return NO;
//    }
    
    [dic setObject:button forKey:key];
    return YES;
}

- (IBAction)nextButtonClick:(id)sender {
    NSMutableDictionary* photoDic = [NSMutableDictionary dictionary];
    
    if (![self checkPhoto:self.certFrontButton errMsg:@"身份证正面照" dic:photoDic key:@"certFront"]) {
        return;
    }
    
    if (![self checkPhoto:self.certBackButton errMsg:@"身份证反面照" dic:photoDic key:@"certBack"]) {
        return;
    }
    
    if (![self checkPhoto:self.headButton errMsg:@"手持身份证照" dic:photoDic key:@"head"]) {
        return;
    }
    
    if (![self checkPhoto:self.shopButton errMsg:@"营业场所照片" dic:photoDic key:@"place"]) {
        return;
    }
    
    
    [photoDic setObject:self.licenseButton forKey:@"license"];
    
//    if (self.licenseButton.buttonStatus != PhotoStatusSuccess) {
//        if (self.licenseButton.image && ![NSString isNilOrEmpty:self.licenseButton.imgStr]) {
//            [photoDic setObject:self.licenseButton forKey:@"license"];
//        }
//    }
    if (self.screenData) {
        NSString* str = [self.screenData objectForKey:KEY_SCREEN_NAME];
        if (![NSString isNilOrEmpty:str]) {
            [HPUIENG showScreen:SCR_QUALITY_CONFIRM params:@{@"confirmFlag": @"1", @"person": self.screenData, @"photoKey": photoDic,KEY_SCREEN_NAME:str}];
            return;
        }
    }
    
    [HPUIENG showScreen:SCR_QUALITY_CONFIRM params:@{@"confirmFlag": @"1", @"person": self.screenData, @"photoKey": photoDic}];
}
@end
